package com.ls.utils;

import com.ls.exception.SysException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 校验三大运营商手机号码
 */
@Slf4j
public class PhoneUtils {
    public static final int PHONE_LENGTH = 11;
    public static final int CALL_TELECOM = 1;
    public static final int CALL_UNICOM = 2;
    public static final int CALL_MOBILE = 3;
    private static final List TELECOM_LIST = Arrays.asList("133", "141", "149", "153", "173", "174", "177", "179", "180", "181", "189", "190", "191", "193", "199");
    private static final List UNICOM_LIST = Arrays.asList("130", "131", "132", "145", "146", "155", "166", "170", "171", "175", "176", "185", "186", "196");
    private static final List MOBILE_LIST = Arrays.asList("134", "135", "136", "137", "138", "139", "147", "150", "151", "152", "157", "158", "159", "172", "182", "183", "184", "187", "188", "195", "197", "198", "178");
    //20220826 add
    public static final String FIXED_PHONE_PREFIX = "0";
    public static final String MOBILE_PHONE_PREFIX = "1";
    public static final String TELECOM_PREFIX = "118114";
    public static final String NOT_TELECOM_PREFIX = "3333";

    /**
     * 返回0的时候说明号段不是三大运营商的
     */
    public static int getPhoneType(String phone) {
        if (StringUtils.isBlank(phone)) {
            throw new SysException("isTelecom param null");
        }
        if (phone.length() != PHONE_LENGTH) {
            throw new SysException("isTelecom param length wrong :" + phone);
        }
        String prefix = phone.substring(0, 3);
        if (TELECOM_LIST.contains(prefix)) {
            return CALL_TELECOM;
        } else if (UNICOM_LIST.contains(prefix)) {
            return CALL_UNICOM;
        } else if (MOBILE_LIST.contains(prefix)) {
            return CALL_MOBILE;
        } else {
            return 0;
        }
    }

    /**
     * 给号码加星号 ,一般是从后往前数，第五个开始
     */
    public static String star(String myPhone, int starNum) {
        if (StringUtils.isBlank(myPhone) || myPhone.length() <= starNum * 2) {
            return myPhone;
        }
//        String[] array = myPhone.
        StringBuilder sb = new StringBuilder();
        sb.append(myPhone.substring(0, myPhone.length() - starNum * 2));
        for (int i = 0; i < starNum; i++) {
            sb.append("*");
        }
        sb.append(myPhone.substring(myPhone.length() - starNum));
        return sb.toString();
    }

    /**
     * 字符串是否包含url
     */
    public static boolean isContainUrl(String context) {
//        String target ="test http://www.test1.com";
        String http = "((http:\\/\\/|https:\\/\\/)?(www.)?(([a-zA-Z0-9-]){2,}\\.){1,4}([a-zA-Z]){2,6}(\\/([a-zA-Z-_\\/\\.0-9#:?=&;,]*)?)?)";
        Pattern pattern = Pattern.compile(http);
        Matcher matcher = pattern.matcher(context);
        while (matcher.find()) {
//            System.out.println(matcher.start() +" :" + matcher.end());
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        String phone = "13212345678";
        String zuoji = "02812345678";
        System.out.println(star(phone, 4));
        System.out.println(star(zuoji, 4));
        System.out.println(isContainUrl("www.baidu.com"));
    }


}
